<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-12-04
 * Time: 17:30
 */

namespace app\store\controller;

use app\comm\service\DadaOpenapi;

class Dada
{
    private $config = [
        'app_key'    => 'dada4909cf24c5c4842',
        'app_secret' => '415720a809c2cf045761553dc6cfc096',
        'source_id'  => '73753'
    ];

    public function addOrder()
    {
//*********************配置项*************************
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/addOrder';

        $obj = new DadaOpenapi($this->config);

//***********************发单接口************************
//发单请求数据,只是样例数据，根据自己的需求进行更改。
        $data = array(
            'shop_no'=> '11047059',
            'origin_id'=> '20171205003',
            'city_code'=> '0532',
            'pay_for_supplier_fee'=> 0.0,
            'fetch_from_receiver_fee'=> 0.0,
            'deliver_fee'=> 0.0,
            'tips'=> 0,
            'info'=> '测试订单',
            'cargo_type'=> 1,
            'cargo_weight'=> 10,
            'cargo_price'=> 10,
            'cargo_num'=> 2,
            'is_prepay'=> 0,
            'expected_fetch_time'=> time()+600,
            'expected_finish_time'=> 0,
            'invoice_title'=> '测试',
            'receiver_name'=> '测试',
            'receiver_address'=> '青岛市黄岛区',
            'receiver_phone'=> '18588888888',
            'receiver_tel'=> '18599999999',
            'receiver_lat'=> 35.956852,
            'receiver_lng'=> 120.166737,
            'callback'=>'http://ysbl.fxshe.com/index.php/store/dada/notify',
        );

//请求接口
        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                echo '成功';
                //返回成功 ....
            }else{
                echo '失败';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 查询订单的相关信息以及骑手的相关信息
     */
    public function query()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/status/query';

        $obj = new DadaOpenapi($this->config);

        $data = [
            'order_id'=> '20171205001',
        ];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                halt($obj->getResult());
                //返回成功 ....
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }


    /**
     * 获取城市列表
     */
    public function cityCode()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/cityCode/list';

        $obj = new DadaOpenapi($this->config);

        $data = [];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                halt($obj->getResult());
                //返回成功 ....
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 订单预发布 1、查询订单运费接口
     * 传入订单相关参数可以查询到该时刻订单所需的运费，同时返回一个唯一的平台订单编号，注意：该平台订单编号有效期为3分钟。
     */
    public function queryDeliverFee()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/queryDeliverFee';

        $obj = new DadaOpenapi($this->config);

        $data = [
            'shop_no'=> '11047059',
            'origin_id'=> '20171205004',
            'city_code'=> '0532',
            'cargo_price'=> 10,
            'is_prepay'=> 0,
            'expected_fetch_time'=> time()+600,
            'receiver_name'=> '测试',
            'receiver_address'=> '青岛市黄岛区峨眉山路光谷软件园27号5层',
            'callback'=>'http://ysbl.fxshe.com/index.php/store/dada/notify',
            'receiver_phone'=> '15318889775'
        ];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                halt($obj->getResult());
                //返回成功 ....
                /*array(3) {
                  ["distance"] => float(542342.52005456)
                  ["fee"] => float(33)
                  ["deliveryNo"] => string(36) "Dada766eeba6819e4381bfb653988fede395"
                }*/
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 2、查询运费后发单接口
     */
    public function addAfterQuery()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/addAfterQuery';

        $obj = new DadaOpenapi($this->config);

        $data = [
            'deliveryNo' => 'Dada190bc33ad4ed40599f13a65a0b313110'
        ];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                echo '成功了，';
                //返回成功 ....
                /*array(3) {
                  ["distance"] => float(542342.52005456)
                  ["fee"] => float(33)
                  ["deliveryNo"] => string(36) "Dada766eeba6819e4381bfb653988fede395"
                }*/
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 在测试环境中，可调用此接口接受订单，检测回调。
     */
    public function accept()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/accept';

        $obj = new DadaOpenapi($this->config);

        $data = [
            'order_id' => '20171205004'
        ];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                echo '成功了，';
                //返回成功 ....
                /*array(3) {
                  ["distance"] => float(542342.52005456)
                  ["fee"] => float(33)
                  ["deliveryNo"] => string(36) "Dada766eeba6819e4381bfb653988fede395"
                }*/
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 订单回调测试，完成取货(仅在测试环境供调试使用)
     */
    public function fetch()
    {
        $this ->config['url'] = 'http://newopen.qa.imdada.cn/api/order/fetch';

        $obj = new DadaOpenapi($this->config);

        $data = [
            'order_id' => '20171205004'
        ];

        $reqStatus = $obj->makeRequest($data);
        if (!$reqStatus) {
            //接口请求正常，判断接口返回的结果，自定义业务操作
            if ($obj->getCode() == 0) {
                echo '成功了，';
                //返回成功 ....
                /*array(3) {
                  ["distance"] => float(542342.52005456)
                  ["fee"] => float(33)
                  ["deliveryNo"] => string(36) "Dada766eeba6819e4381bfb653988fede395"
                }*/
            }else{
                echo '失败了，';
                //返回失败
            }
            echo sprintf('code:%s，msg:%s', $obj->getCode(), $obj->getMsg());
        }else{
            //请求异常或者失败
            echo 'except';
        }
    }

    /**
     * 回调接口
     */
    public function notify(){
        $post = file_get_contents('php://input');   //接受的数据

        /*{"order_status":2,"cancel_reason":"","update_time":1512639138,"cancel_from":0,"dm_id":666,"signature":"59c6c64fa84cf7bf20541b4191615ebb","dm_name":"测试达达","order_id":"20171205003","client_id":"","dm_mobile":"13546670420"}*/
        /*client_id	String	是	返回达达运单号，默认为空
        order_id	String	是	添加订单接口中的origin_id值
        order_status	int	是	订单状态(待接单＝1 待取货＝2 配送中＝3 已完成＝4 已取消＝5 已过期＝7 指派单=8 妥投异常之物品返回中=9 妥投异常之物品返回完成=10 创建达达运单失败=1000 可参考文末的状态说明）
        cancel_reason	String	是	订单取消原因,其他状态下默认值为空字符串
        cancel_from	Int	是	订单取消原因来源(1:达达配送员取消；2:商家主动取消；3:系统或客服取消；0:默认值)
        update_time	Int	是	更新时间,时间戳
        signature	String	是	对client_id, order_id, update_time的值进行字符串升序排列，再连接字符串，取md5值*/

        db('dada') -> insert([
            'content' => $post,
            'addtime' => time()
        ]);
    }

}